package com.chengcang.admin.modules.log.controller;

import com.chengcang.admin.common.annotation.LogOperation;
import com.chengcang.admin.common.utils.ExcelUtils;
import com.chengcang.admin.modules.log.dto.SysLogOperateDTO;
import com.chengcang.admin.modules.log.excel.SysLogOperateExcel;
import com.chengcang.admin.modules.log.service.SysLogOperateService;
import com.chengcang.common.constant.Constant;
import com.chengcang.common.page.PageData;
import com.chengcang.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 操作日志
 *
 * @author Zhanli xy321231@163.com
 * @since 1.0.0 2023-01-31
 */
@RestController
@RequestMapping("busi/log/operate")
@Api(tags = "操作日志1")
@Slf4j
public class SysLogOperateController {
    @Autowired
    private SysLogOperateService sysLogOperateService;

    @Value("${binlog.table}")
    private String table;


    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "index", value = "对于数据库表 展示为tableId-tableName，传输 传输下标" +
                    " 0-busi_address,1-busi_demand,2-busi_demand_tenant,3-busi_finance,4-busi_invoice_head,5-busi_order" +
                    ",6-sys_area,7-sys_dept,8-sys_dict_data,9-sys_dict_type,10-sys_menu,11-sys_oss,12-sys_params,13-sys_product,14-sys_product_type,15-sys_role" +
                    ",16-sys_role_data_scope,17-sys_role_menu,18-sys_role_user,19-sys_tenant,20-sys_tenant_bindings,21-sys_tenant_menu,22-sys_user,23-sys_user_tenant,24-sys_user_token", paramType = "query", dataType = "String")
    })
//    @RequiresPermissions("busi:syslogoperate:page")
    public Result<PageData<SysLogOperateDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<String> tableList = Arrays.asList(table.split(","));
        log.info(0+"==="+tableList.get(0),tableList);
        if (!Objects.isNull(params.get("index"))) {
            params.put("tableName", tableList.get(Integer.parseInt(String.valueOf(params.get("index")))));
            log.info("查询下标：{} 对应表{}，操作日志！", params.get("index"), tableList.get(Integer.parseInt(String.valueOf(params.get("index")))));
        }
        PageData<SysLogOperateDTO> page = sysLogOperateService.page(params);

        return new Result<PageData<SysLogOperateDTO>>().ok(page);
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
//    @RequiresPermissions("busi:syslogoperate:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {


        List<SysLogOperateDTO> list = sysLogOperateService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, SysLogOperateExcel.class);
    }

}